<template>
  <div class="app-container">
      通知查询
    <el-row type="flex" class="row-bg" justify="center" :gutter="20">
      <el-col :span="12">
        <el-select v-model="param.targetType" placeholder="请选择" clearable>
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value" >
          </el-option>
        </el-select>
        <el-input
        placeholder="请输入标题"
        prefix-icon="el-icon-search" clearable v-model="param.noticeTitle">
        </el-input>
      </el-col>
      <el-col :span="4">
    <el-button icon="el-icon-search" circle @click="selectNotice"></el-button>
      </el-col>
    </el-row>
    <el-button type="primary" @click="addNotice">新建通知</el-button>
    <el-row>
    <el-table :data="notifyList" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="通知标题" >
        <template slot-scope="scope">
          {{ scope.row.noticeTitle }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="通知类型" width="100">
        <template slot-scope="scope">
          {{ scope.row.noticeType }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="通知发布对象" width="100">
        <template slot-scope="scope">
          {{ scope.row.targetType }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="通知发布时间" width="100">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="toDetail(scope.row)">详情</el-button>
          <el-button type="primary" size="small" @click="updateNotice(scope.row)">修改</el-button>
          <el-button type="primary" size="small" @click="deleteNotice(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      
    </el-table>
    </el-row>
    <!--分页组件-->
<el-pagination background layout="prev, pager, next"  prev-text="上一页"
        next-text="下一页"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currpage"
        :page-size="pageSize"
        :total="total"></el-pagination>

  </div>
</template>

<script>
import { getNoticeList,deleteNotice,updateNotice} from '@/api/notice'
import { deepClone } from '@/utils'


export default {
  data() {
    return {
      notifyList: [],
      dialogVisible: false,
      dialogTitle: '',
      roleList: [],
      checkedList: [],
      param :{
      start: 0,
      limit: 10,
      noticeId:'',
      targetType:'',
      noticeTitle:''
      },
      pageSize: 10, //每一页的数据大小
      currpage: 1, //当前页
      total:0,//总条数
       options: [{
          value: '0',
          label: '公开'
        }, {
          value: '1',
          label: '用户'
        }, {
          value: '2',
          label: '学生'
        }, {
          value: '3',
          label: '教师'
        }],
        value: ''
    }
  },
  //页面打开时执行的方法
  created() {
    this.getNoticeList()
  },
  methods: {
    async getNoticeList() {
      let res = await getNoticeList(this.param)
      this.notifyList = res.beans
      this.total = res.bean.total
    },
    //页大小变化
     handleSizeChange(val) {
      this.pageSize = val;
      this.param.start = this.pageSize*(this.currpage-1)
      this.getNoticeList();
    },
    //页标号变化
    handleCurrentChange(val) {
      this.currpage = val;
      this.param.start = this.pageSize*(this.currpage-1)
      this.getNoticeList();
    },
    selectNotice(){
      this.param.start = 0;
      this.param.limit = 10;
      this.currpage = 1;
      this.getNoticeList()
    },
    toDetail(row) {
     this.$router.push({
        path: "/noticeDetail",
        query:{
          noticeId:row.noticeId
        }
        })
    },
    addNotice(row) {
     this.$router.push("/addNotice")
    },
    async deleteNotice(row) {
      this.param.noticeId = row.noticeId
      await deleteNotice(this.param).then(()=>{
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            }
          ).catch(err=>{
            this.$message({
              type: 'error',
              message: '删除失败!'
            })
            })
    },
    updateNotice(row) {
     this.$router.push({
        path: "/editNotice",
        query:{
          noticeId:row.noticeId
        }
        })
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
